package com.jwcao.common.cast.operation.injection;

import com.jwcao.common.cast.operation.access.AccessOperation;
import com.jwcao.common.scan.object.ScanClass;
import java.util.List;

/**
 * name: MethodInjectionOperation description: TODO
 *
 * @author caojingwei date: 2023/3/23 17:39
 */
public class MethodInjectionOperation extends InjectionOperation {

    protected final List<AccessOperation> accessOperationList;
    private final String name;

    public MethodInjectionOperation(ScanClass targetClass,
        List<AccessOperation> accessOperationList, String name) {
        super(targetClass);
        this.accessOperationList = accessOperationList;
        this.name = name;
    }

    @Override
    public void execute(Object sourceObject, Object targetObject) {
        Object[] parameters = accessOperationList.stream().map(a->a.execute(sourceObject)).toArray();
        targetClass.invokeVoidMethod(targetObject, name, parameters);
    }
}
